﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : Mojito\Mojito.Service\DishesCategoryService.cs
// Author : Sohobloo
// Create time : 2014/08/19 10:57
// Update time : 2014/08/19 11:06

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Mojito.Data.Repository;
using Mojito.Entity;
using Sohobloo.SoCommon;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoMcCoffee.Entity;
using Sohobloo.SoMcCoffee.Extension;
using Sohobloo.SoMcCoffee.Service;

#endregion

namespace Mojito.Service.Impl
{
    [Export(typeof(IDishCategoryService))]
    internal class DishCategoryService : ServiceBase, IDishCategoryService
    {
        [Import]
        private IDishCategoryRepository DishCategoryRepository { get; set; }

        [Import]
        private IDishRepository DishesRepository { get; set; }

        public DishCategory GetDishCategory(Guid dishesCategoryId)
        {
            return DishCategoryRepository.GetByKey(dishesCategoryId);
        }

        public IList<DishCategory> GetDishCategoryList()
        {
            return DishCategoryRepository.Entities
                                           .Where(t => t.DataStatus != DataStatus.Deleted)
                                           .OrderBy(t => t.SortIndex)
                                           .ToList();
        }

        public OperationResult AddDishCategory(string categoryName)
        {
            ArgumentHelper.Instance
                          .CheckEmpty(categoryName);
            var existed = DishCategoryRepository.Entities
                                                .Any(t => t.CategoryName == categoryName
                                                          && t.DataStatus != DataStatus.Deleted);
            if(existed)
            {
                return new OperationResult(OperationResultType.Exist, "Category name exist.");
            }

            var dishCategory = new DishCategory
            {
                CategoryName = categoryName
            };

            DishCategoryRepository.Insert(dishCategory);
            var result = Commit();
            result.AppendData = dishCategory;
            return result;
        }

        public OperationResult UpdateDishCategory(Guid dishCategoryId, string categoryName)
        {
            ArgumentHelper.Instance.CheckEmpty(categoryName);
            var existed = DishCategoryRepository.Entities
                                                .Any(t => t.CategoryName == categoryName
                                                          && t.DataStatus != DataStatus.Deleted
                                                          && t.Id != dishCategoryId);
            if(existed)
            {
                return new OperationResult(OperationResultType.Exist, "Category name exist.");
            }
            var dishCategory = DishCategoryRepository.GetByKey(dishCategoryId);
            if(dishCategory.IsNull())
            {
                return new OperationResult(OperationResultType.NotExist, "Category not exist.");
            }

            dishCategory.CategoryName = categoryName;

            DishCategoryRepository.Update(dishCategory);
            var result = Commit();
            result.AppendData = dishCategory;
            return result;
        }

        public OperationResult DeleteDishCategory(Guid dishCategoryId)
        {
            // 检查引用
            var dishCategory = DishCategoryRepository.Entities
                .Include(d => d.Dishes)
                .SingleOrDefault(d => d.Id == dishCategoryId);
            if (dishCategory.IsNull())
            {
                return new OperationResult(OperationResultType.NotExist, "Category not exist.");
            }

            if (dishCategory.Dishes.Any())
            {
                // 解除引用
                foreach (var dish in dishCategory.Dishes)
                {
                    dish.CategoryId = null;
                }
                DishesRepository.Update(dishCategory.Dishes);
            }
            // 硬删除
            DishCategoryRepository.Delete(dishCategoryId);
            return Commit();
        }

        public OperationResult SortDishCategory(Guid dishCategoryId, Guid? prevDishCategoryId)
        {
            DishCategoryRepository.ChangeSort(dishCategoryId, prevDishCategoryId);
            return Commit();
        }
    }
}